#include 	"receive.h"

static void GPIO_config() {
  	//P46 P47 双准向
      P1_MODE_IO_PU(GPIO_Pin_0 | GPIO_Pin_1);
}

static void UART_config(void) {
	// >>> 记得添加 NVIC.c, UART.c, UART_Isr.c <<<
    COMx_InitDefine		COMx_InitStructure;					//结构定义
    COMx_InitStructure.UART_Mode      = UART_8bit_BRTx;	//模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
    COMx_InitStructure.UART_BRT_Use   = BRT_Timer2;			//选择波特率发生器, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
    COMx_InitStructure.UART_BaudRate  = 115200ul;			//波特率, 一般 110 ~ 115200
    COMx_InitStructure.UART_RxEnable  = ENABLE;				//接收允许,   ENABLE或DISABLE
    COMx_InitStructure.BaudRateDouble = DISABLE;			//波特率加倍, ENABLE或DISABLE
    UART_Configuration(UART2, &COMx_InitStructure);		//初始化串口1 UART1,UART2,UART3,UART4

  	NVIC_UART2_Init(ENABLE,Priority_1);		//中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
    UART2_SW(UART2_SW_P10_P11);	// 引脚选择, UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17,UART1_SW_P43_P44
}
void Receive_Init(void){
    GPIO_config(); // gpio
    UART_config(); // uart
}

void Receive_Get(){
    u8 i;
    while (1)
    {
         // UART2收到数据RX4_Buffer[i]，通过UART1发出去 TX1_write2buff
         if(COM2.RX_TimeOut > 0) {
            //超时计数
            if(--COM2.RX_TimeOut == 0) {
                if(COM2.RX_Cnt > 0) {
                    for(i=0; i<COM2.RX_Cnt; i++)	{
                        // UART4收到数据RX4_Buffer[i]，通过UART1发出去 TX1_write2buff
                        //TX1_write2buff(RX2_Buffer[i]);
                    }
                    switch (RX2_Buffer[0])
                    {
                    case 0x10:
                        /*前进*/
                        Motors_forward(25, MID_M);
                        break;
                    case 0x11:
                        /*后退*/
                        Motors_backward(25, MID_M);
                        break;
                    case 0x12:
                        /*左漂*/
                        Motors_translate(25, LEFT_M);
                        break;
                    case 0x13:
                        /*右漂*/
                        Motors_translate(25, RIGHT_M);
                        break;
                    case 0x14:
                        /*停车*/
                        Motors_stop();
                        break;
                    case 0x15:
                        /*左前*/
                        Motors_forward(25, LEFT_M);
                        break;
                    case 0x16:
                        /*右前*/
                        Motors_forward(25, RIGHT_M);
                        break;
                    case 0x17:
                        /*左后*/
                        Motors_backward(25, LEFT_M);
                        break;
                    case 0x18:
                        /*右后*/
                        Motors_backward(25, RIGHT_M);
                        break;
                    default:
                        break;
                    }
                }
                COM2.RX_Cnt = 0;
            }
        }
    }
    
}